var ctx1=''
Page({

  /**
   * 页面的初始数据
   */
  data: {fileList: [], list:[],fileList1:[],url:'',url1:'',near_id:'',goods_id:'',last:'',type:'',
  winwidth:'',winheight:'',ishidden:false,show:false,url3:'',url4:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
       this.setData({
         near_id:options.near_id,
         goods_id:options.goods_id,
         type:options.type
       })
       wx.getSystemInfo({
              complete: (res) => {
                console.log(res);
                this.setData({
                  winwidth:res.windowWidth,
                  winheight:res.windowHeight
                })
              },
            })
            this.ctx = wx.createCameraContext()
            ctx1 = wx.createCanvasContext('myCanvas')
  },
  onShow(){
    wx.uploadFile({
      filePath: this.data.url,
      name: 'file',
      url: 'https://zujieba.com/Api/upload/upload.html',
      success:(res)=>{
        let data=JSON.parse(res.data).name
        this.setData({
          url3:data
        })}})
        wx.uploadFile({
          filePath: this.data.url1,
          name: 'file',
          url: 'https://zujieba.com/Api/upload/upload.html',
          success:(res)=>{
            let data=JSON.parse(res.data).name
            this.setData({
              url4:data
            })}})
  },
  takePhoto() {
        wx.showLoading({
          title: '正在生成图片,请稍后',
          mask:true
        })
        this.ctx.takePhoto({
          quality: 'high',
          success: (res) => {
            console.log(res);
            
            this.setData({
              src: res.tempImagePath
            })
            if(this.data.type==1){
              this.canvasimg(res.tempImagePath,237,150)
            }
            if(this.data.type==2){
              this.canvasimg(res.tempImagePath,217,150)
            }
            if(this.data.type==3){
              this.canvasimg(res.tempImagePath,300,220)
            }

           
          }
        })
      },
      canvasimg(file,wid,hei){
        var that=this
        wx.getImageInfo({
          src: file,
          success:(res)=>{
            console.log(res);
            let width=res.width
            let height=res.height
            ctx1.drawImage(file,(that.data.winwidth-wid)/2/that.data.winwidth*width,(that.data.winheight-hei)/2/that.data.winheight*height,wid/that.data.winwidth*width,hei/that.data.winheight*height,0,0,wid,hei)
            ctx1.draw(true,function(){
              wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                // width: 237,
                // height: 150,
               width:wid*4,
               height:hei*4,
                canvasId: 'myCanvas',
                quality:1,
                fileType:'jpg',
                success(res) {
                  console.log(res);
                  wx.uploadFile({
                    filePath: res.tempFilePath,
                    name: 'file',
                    url: 'https://zujieba.com/Api/upload/upload.html',
                    success:(res1)=>{console.log(res1);
                      let res2=JSON.parse(res1.data)
                      console.log(res2);
                       wx.hideLoading()
                      console.log('https://zujieba.com/uploads/'+res2.name);
                      // that.setData({
                      //   url:'https://zujieba.com/uploads/'+res2.name,
                      //   ishidden:false
                      // })
                      if(that.data.status==1){
                        that.setData({
                            url:'https://zujieba.com/uploads/'+res2.name,
                            ishidden:false,
                            url3:res2.name
                          })
                      }
                      if(that.data.status==2){
                        that.setData({
                            url1:'https://zujieba.com/uploads/'+res2.name,
                            ishidden:false,
                            url4:res2.name
                          })
                      }
                    }})
                
                
                }})
            })
          }
        })
        
       
      },
      toggle(){
        this.setData({
          ishidden:true
        })
      },

  getimage(){
    wx.chooseImage({
      count:1,
      sourceType:['album'],
      complete: (res) => {
        console.log(res);
        let file=res.tempFiles[0].path
        this.setData({
          url:file
        })
        wx.uploadFile({
          filePath: file,
          name: 'file',
          url: 'https://zujieba.com/Api/upload/upload.html',
          success:(res)=>{
            let data=JSON.parse(res.data).name
            this.setData({
              url3:data
            })

          }})
        
      },
    })

  },
  getimageall(){
    this.setData({
      show:false
    })
     if(this.data.status==1){
       this.getimage()
     }
     if(this.data.status==2){
      this.getimage1()
     }
  },
  getimage1(){
    wx.chooseImage({
      sourceType:['album'],
      count:1,
      success: (res) => {
        console.log(res);
        let file=res.tempFiles[0].path
        wx.uploadFile({
          filePath: file,
          name: 'file',
          url: 'https://zujieba.com/Api/upload/upload.html',
          success:(res)=>{
            let data=JSON.parse(res.data).name
            this.setData({
              url4:data
            })

          }})



        this.setData({
          url1:file
        })
        
        
      },
    })
  },
  del(){
   this.setData({
     url:'',
     url3:''
   })
  },
  del1(){
    this.setData({
      url1:'',
      url4:''
    })
   },
   navcanvas(e){
       wx.navigateTo({
         url: '/pages/edit5/edit5?url='+e.currentTarget.dataset.url+"&flag=4",
       })
   },
   navcanvas1(e){
    wx.navigateTo({
      url: '/pages/edit5/edit5?url='+e.currentTarget.dataset.url+"&flag=5",
    })
},

submit(){
  
  
   if(this.data.url==''||this.data.url1==''){return wx.showToast({
     title: '请上传完整图片',
     icon:'none'
   })}
   wx.showLoading({
    title: '正在上传中...',
  })
   wx.uploadFile({
     filePath: this.data.url,
     name: 'file',
     url: 'https://zujieba.com/Api/upload/upload.html',
     success:(res)=>{
       console.log(res);
       
        wx.uploadFile({
          filePath: this.data.url1,
          name: 'file',
          url: 'https://zujieba.com/Api/upload/upload.html',
          success:(res1)=>{
               let data=JSON.parse(res.data).name
               let data1=JSON.parse(res1.data).name
               wx.request({
                url: 'https://zujieba.com/Api/upload/canvas',
                data:{pic:data,img:data1,type:this.data.type},
                method:'POST',
                success:(result)=>{
                  console.log(result);
                  
                  if(result.data.success==1){
                    wx.hideLoading()
                     
                  console.log(result);
                 let url="https://zujieba.com/uploads/"+ result.data.data
                 console.log(url);
                 
                this.throttle(()=>{ wx.navigateTo({
                  url: '/pages/fileprint/print/print?url='+url+'&near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+'&time='+result.data.addtime+"&name="+result.data.data+'&photo=1'
                })},2000)
                }else{
                  wx.hideLoading()
                  wx.showToast({
                    title: result.data.tips,
                    icon:'none'
                  })
                }
                }
              })

          }
        })
     }
   })
  
 
},
submit1(){
   if(!this.data.url3&&!this.data.url4){
     wx.showToast({
       title: '请上传照片',
       icon:'none'
     })
     return
   }
  wx.request({
    url: 'https://zujieba.com/Api/upload/canvas',
    data:{pic:this.data.url3,img:this.data.url4,type:this.data.type},
    method:'POST',
    success:(result)=>{
      console.log(result);
      
      if(result.data.success==1){
        wx.hideLoading()
         
      console.log(result);
     let url="https://zujieba.com/uploads/"+ result.data.data
     console.log(url);
     
    this.throttle(()=>{ 
    //   wx.navigateTo({
    //   url: '/pages/fileprint/print/print?url='+url+'&near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+'&time='+result.data.addtime+"&name="+result.data.data+'&photo=1'
    // })
    wx.navigateTo({
      url: '/pages/fileprint/print/newprint?url='+url+'&near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+'&time='+result.data.addtime+"&name="+result.data.data+'&photo=1'+'&photoid=1'
    })
  },2000)
    }else{
      wx.hideLoading()
      wx.showToast({
        title: result.data.tips,
        icon:'none'
      })
    }
    }
  })
},
 //节流
 throttle:function(func,wait){
  console.log(func,wait);
  
  let last =this.data.last
  console.log(last);
  
  let current_time= +new Date()
  if(current_time-last>wait){
    console.log(1111);
      console.log(current_time,current_time-last);
      
      func()
      last=+new Date()
      this.setData({
        last
      })
  }},
  get2(){
    this.setData({ishidden:true})
  },
  get1(){
    this.setData({ishidden:true,show:false})
  },
  onClose() {
    this.setData({ show: false });
  },
  showmodal(){
    this.setData({show:true,status:1})
  },
  showmodal1(){
    this.setData({show:true,status:2})
  }
})



// 自定义相机
// var ctx1=''

// Page({

//   data: {winwidth:'',winheight:'',ishidden:false
    
//   },

//   onLoad: function (options) {
//     wx.getSystemInfo({
//       complete: (res) => {
//         console.log(res);
//         this.setData({
//           winwidth:res.windowWidth,
//           winheight:res.windowHeight
//         })
//       },
//     })
//     this.ctx = wx.createCameraContext()
//     ctx1 = wx.createCanvasContext('myCanvas')
//   },
//   takePhoto() {
//     wx.showLoading({
//       title: '正在生成图片,请稍后',
//       mask:true
//     })
//     this.ctx.takePhoto({
//       quality: 'high',
//       success: (res) => {
//         console.log(res);
        
//         this.setData({
//           src: res.tempImagePath
//         })
//         this.canvasimg(res.tempImagePath)
//       }
//     })
//   },
//   canvasimg(file){
//     var that=this
//     wx.getImageInfo({
//       src: file,
//       success:(res)=>{
//         console.log(res);
//         let width=res.width
//         let height=res.height
//         ctx1.drawImage(file,(that.data.winwidth-237)/2/that.data.winwidth*width,(that.data.winheight-150)/2/that.data.winheight*height,237/that.data.winwidth*width,150/that.data.winheight*height,0,0,237,150)
//         ctx1.draw(true,function(){
//           wx.canvasToTempFilePath({
//             x: 0,
//             y: 0,
//             width: 237,
//             height: 150,
            
//             canvasId: 'myCanvas',
//             quality:1,
//             fileType:'jpg',
//             success(res) {
//               console.log(res);
//               wx.uploadFile({
//                 filePath: res.tempFilePath,
//                 name: 'file',
//                 url: 'https://zujieba.com/Api/upload/upload.html',
//                 success:(res1)=>{console.log(res1);
//                   let res2=JSON.parse(res1.data)
//                   console.log(res2);
//                    wx.hideLoading()
//                   console.log('https://zujieba.com/uploads/'+res2.name);
//                   that.setData({
//                     url:'https://zujieba.com/uploads/'+res2.name,
//                     ishidden:false
//                   })
//                 }})
            
            
//             }})
//         })
//       }
//     })
    
   
//   },
//   toggle(){
//     this.setData({
//       ishidden:true
//     })
//   }
 
// })
